﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PressureTest
{
    public partial class EditTemplateForm: Form
    {

        /// <summary>模型
        /// 
        /// </summary>
        public Template obj = new Template();

        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = NumberTextBox.Text;
            
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            int count = list.Count;
            if (count > 0)
            {
                Alert.Error(where["number"] + "模板编号已存在");
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>更新obj
        /// 
        /// </summary>
        /// <returns></returns>
        private Template UpdateObj()
        {
            obj.number = NumberTextBox.Text.ToInt();
            obj.pipe_position = PipePositionComboBox.Text;
            obj.design_pressure = DesignPressureTextBox.Text.ToFloat();
            if (EditEnableComboBox.Text == "是")
            {
                obj.edit_enable = 1;
            }
            else
            {
                obj.edit_enable = 0;
            }
            obj.first_stage_pressure = FirstStagePressureTextBox.Text.ToFloat();
            obj.first_stage_minute = FirstStageMinuteTextBox.Text.ToInt();
            obj.first_stage_estimate = FormatEstimate(FirstStageEstimateComboBox.Text);
            obj.first_stage_estimate_value = FirstStageEstimateValueTextBox.Text.ToFloat();

            obj.second_stage_pressure = SecondStagePressureTextBox.Text.ToFloat();
            obj.second_stage_minute = SecondStageMinuteTextBox.Text.ToInt();
            obj.second_stage_estimate = FormatEstimate(SecondStageEstimateComboBox.Text);
            obj.second_stage_estimate_value = SecondStageEstimateValueTextBox.Text.ToFloat();

            obj.third_stage_pressure = ThirdStagePressureTextBox.Text.ToFloat();
            obj.third_stage_minute = ThirdStageMinuteTextBox.Text.ToInt();
            obj.third_stage_estimate = FormatEstimate(ThirdStageEstimateComboBox.Text);
            obj.third_stage_estimate_value = ThirdStageEstimateValueTextBox.Text.ToFloat();

            obj.rigor_stage_pressure = RigorStagePressureTextBox.Text.ToFloat();
            obj.rigor_stage_minute = RigorStageMinuteTextBox.Text.ToInt();
            obj.rigor_stage_estimate = FormatEstimate(RigorStageEstimateComboBox.Text);
            obj.rigor_stage_estimate_value = RigorStageEstimateValueTextBox.Text.ToFloat();

            obj.intensity_stage_pressure = IntensityStagePressureTextBox.Text.ToFloat();
            obj.intensity_stage_minute = IntensityStageMinuteTextBox.Text.ToInt();
            obj.intensity_stage_estimate = FormatEstimate(IntensityStageEstimateComboBox.Text);
            obj.intensity_stage_estimate_value = IntensityStageEstimateValueTextBox.Text.ToFloat();
            return obj;
        }

        /// <summary>刷新
        /// 
        /// </summary>
        private void Fresh()
        {
            NumberTextBox.Text = obj.number.ToString();
            PipePositionComboBox.SelectedItem = obj.pipe_position;
            DesignPressureTextBox.Text = obj.design_pressure.ToString();
            string edit_enable = "否";
            if (obj.edit_enable == 1)
            {
                edit_enable = "是";
            }
            EditEnableComboBox.SelectedItem = edit_enable;

            FirstStagePressureTextBox.Text = obj.first_stage_pressure.ToString();
            FirstStageMinuteTextBox.Text = obj.first_stage_minute.ToString();
            string estimate = "固定数值";
            if (obj.first_stage_estimate == 1)
            {
                estimate = "浮动范围";
            }
            FirstStageEstimateComboBox.Text = estimate;
            FirstStageEstimateValueTextBox.Text = obj.first_stage_estimate_value.ToString();

            SecondStagePressureTextBox.Text = obj.second_stage_pressure.ToString();
            SecondStageMinuteTextBox.Text = obj.second_stage_minute.ToString();
            estimate = "固定数值";
            if (obj.second_stage_estimate == 1)
            {
                estimate = "浮动范围";
            }
            SecondStageEstimateComboBox.Text = estimate;
            SecondStageEstimateValueTextBox.Text = obj.second_stage_estimate_value.ToString();

            ThirdStagePressureTextBox.Text = obj.third_stage_pressure.ToString();
            ThirdStageMinuteTextBox.Text = obj.third_stage_minute.ToString();
            estimate = "固定数值";
            if (obj.third_stage_estimate == 1)
            {
                estimate = "浮动范围";
            }
            ThirdStageEstimateComboBox.Text = estimate;
            ThirdStageEstimateValueTextBox.Text = obj.third_stage_estimate_value.ToString();

            RigorStagePressureTextBox.Text = obj.rigor_stage_pressure.ToString();
            RigorStageMinuteTextBox.Text = obj.rigor_stage_minute.ToString();
            estimate = "固定数值";
            if (obj.rigor_stage_estimate == 1)
            {
                estimate = "浮动范围";
            }
            RigorStageEstimateComboBox.Text = estimate;
            RigorStageEstimateValueTextBox.Text = obj.rigor_stage_estimate_value.ToString();

            IntensityStagePressureTextBox.Text = obj.intensity_stage_pressure.ToString();
            IntensityStageMinuteTextBox.Text = obj.intensity_stage_minute.ToString();
            estimate = "固定数值";
            if (obj.intensity_stage_estimate == 1)
            {
                estimate = "浮动范围";
            }
            IntensityStageEstimateComboBox.Text = estimate;
            IntensityStageEstimateValueTextBox.Text = obj.intensity_stage_estimate_value.ToString();
        }

        /// <summary>操作成功
        /// 
        /// </summary>
        private void OperationSuccess()
        {
            ManageTemplateForm frm = (ManageTemplateForm)this.Owner;
            frm.Fresh();
            this.Close();
        }

        public EditTemplateForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        /// <summary>确定
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (NumberTextBox.Text != obj.number.ToString())
            {
                bool repeatRes = CheckRepeat();
                if (repeatRes)
                {
                    return;
                }
            }
            
            UpdateObj();
            List<Template> list = new List<Template>();
            list.Add(obj);
            int res = SqlSugarHelper.Updateable<Template>(list);
            if (res > 0)
            {
                OperationSuccess();
            }
            else
            {
                Alert.Error("编辑点位失败");
            }
        }

        /// <summary>格式化判断方式
        /// 
        /// </summary>
        /// <param name="estimate"></param>
        /// <returns></returns>
        private int FormatEstimate(string str)
        {
            if (str == "固定数值")
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }

        private void EditTemplateForm_Load(object sender, EventArgs e)
        {
            if (obj.number > 0)
            {
                Dictionary<string, Object> where = new Dictionary<string, Object>();
                where["number"] = obj.number;

                List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
                if (list.Count > 0)
                {
                    obj = list[0];
                    Fresh();
                }
                else
                {
                    this.Close();
                    Alert.Error("模板记录不存在，无法编辑");
                }
            }
        }
    }
}
